package top.lixunda.ecommerce.server.good.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.common.validate.XumValid;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.good.api.entity.db.SimpleReviewDO;
import top.lixunda.ecommerce.server.good.api.entity.db.SimpleReviewDetailDO;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.SimpleReviewInsertDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.SimpleReviewQueryDTO;
import top.lixunda.ecommerce.server.good.api.entity.vo.SimpleReviewVO;
import top.lixunda.ecommerce.server.good.api.controller.ISimpleReviewController;
import top.lixunda.ecommerce.server.good.service.ISimpleReviewService;

import java.util.List;

/**
 * @author Xumda
 * @version time:2020/1/14 16:15
 */
@Slf4j
@RestController
public class SimpleReviewController extends BaseController implements ISimpleReviewController {

    private final ISimpleReviewService simpleReviewService;

    @Autowired
    public SimpleReviewController(ISimpleReviewService simpleReviewService) {
        this.simpleReviewService = simpleReviewService;
    }

    /**
     * 根据id查询单个评论信息
     *
     * @param id 评论id
     * @return 评论信息
     */
    @Override
    public MessageObject<SimpleReviewVO> queryReviewById(Integer id) {
        SimpleReviewDetailDO simpleReviewDO = simpleReviewService.queryReviewById(id);
        SimpleReviewVO simpleReviewVO = SimpleReviewVO.fromSimpleReviewDetailDO(simpleReviewDO);
        return getSuccessMessageObject("获取数据成功", simpleReviewVO);
    }

    /**
     * 根据多个id查询多个评论信息
     *
     * @param ids 多个评论id
     * @return 多个评论信息
     */
    @Override
    public MessageObject<List<SimpleReviewVO>> queryReviewsByIds(List<Integer> ids) {
        List<SimpleReviewDetailDO> simpleReviewDO = simpleReviewService.queryReviewsByIds(ids);
        List<SimpleReviewVO> simpleReviewVO = SimpleReviewVO.fromSimpleReviewDetailDO(simpleReviewDO);
        return getSuccessMessageObject("获取数据成功", simpleReviewVO);
    }


    /**
     * 查询某个商品的简单评论
     *
     * @param id 商品id
     * @return 评论信息
     */
    @Override
    public MessageObject<List<SimpleReviewVO>> queryReviewByGoodId(Integer id) {
        List<SimpleReviewDetailDO> simpleReviewDO = simpleReviewService.queryReviewByGoodId(id);
        List<SimpleReviewVO> simpleReviewVO = SimpleReviewVO.fromSimpleReviewDetailDO(simpleReviewDO);
        return getSuccessMessageObject("获取数据成功", simpleReviewVO);
    }

    /**
     * 分页查询某个商品的简单评论
     *
     * @param pageDTO 分页数据，包含条件
     * @return 分页数据
     */
    @Override
    public MessageObject<IPage<SimpleReviewVO>> queryReviewWrapperPage(PageDTO<SimpleReviewDetailDO, SimpleReviewQueryDTO> pageDTO) {
        IPage<SimpleReviewDetailDO> page = simpleReviewService.queryReviewWrapperPage(pageDTO);
        IPage<SimpleReviewVO> resultPage = pageConvert(page, SimpleReviewVO::fromSimpleReviewDetailDO);
        return getSuccessMessageObject("获取分页数据成功", resultPage);
    }

    /**
     * 对某一个商品添加一条评论信息
     *
     * @param simpleReviewInsertDTO 商品评论信息
     * @return 添加成功的评论信息
     */
    @Override
    public MessageObject<SimpleReviewVO> addReview(
            @XumValid SimpleReviewInsertDTO simpleReviewInsertDTO) {
        SimpleReviewDO simpleReviewDO = simpleReviewService.addReview(simpleReviewInsertDTO);
        SimpleReviewVO simpleReviewVO = SimpleReviewVO.fromSimpleReviewDO(simpleReviewDO);
        return getSuccessMessageObject("评论成功", simpleReviewVO);
    }
}
